package com.sailfish.springbootdemo.service.db1;

import com.sailfish.springbootdemo.dao.db1.PcbaDeviceParamDao;
import com.sailfish.springbootdemo.dao.db1.PcbaDeviceRelatedDeviceParamDao;
import com.sailfish.springbootdemo.dao.db1.PcbaDeviceResourceDao;
import com.sailfish.springbootdemo.dao.db1.TechDeviceParamDao;
import com.sailfish.springbootdemo.pojo.Result;
import com.sailfish.springbootdemo.pojo.db1.PcbaDeviceParam;
import com.sailfish.springbootdemo.pojo.db1.PcbaDeviceRelatedDeviceParam;
import com.sailfish.springbootdemo.pojo.db1.PcbaDeviceResource;
import com.sailfish.springbootdemo.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CommonTriggerStatusServiceImpl implements CommonTriggerStatusService {

    @Autowired
    private PcbaDeviceResourceDao pcbaDeviceResourceDao;

    @Autowired
    private PcbaDeviceParamDao pcbaDeviceParamDao;

    @Autowired
    private PcbaDeviceRelatedDeviceParamDao pcbaDeviceRelatedDeviceParamDao;

    @Autowired
    private TechDeviceParamDao techDeviceParamDao;

    @Override
    public Result changeBaseDeviceResourceStatus(Integer pcbaDeviceTypeId, Integer pcbaDeviceParamId) {
        try {
            if ((pcbaDeviceTypeId == null || "".equals(pcbaDeviceTypeId.toString())) && (pcbaDeviceParamId == null || "".equals(pcbaDeviceParamId.toString()))) {
                return ResultUtil.error(500, "缺少参数", "Miss Param", null);
            }
            List<Integer> needChangeResourceIdListTo1 = new ArrayList<>();
            List<Integer> needChangeResourceIdListTo2 = new ArrayList<>();

            if (pcbaDeviceTypeId == null || "".equals(pcbaDeviceTypeId.toString())) {
                PcbaDeviceParam pcbaDeviceParam = pcbaDeviceParamDao.getPcbaDeviceParamById(pcbaDeviceParamId);
                pcbaDeviceTypeId = pcbaDeviceParam.getPcbaDeviceTypeId();
            }

            //根据 pcbaDeviceTypeId 查询 所有设备资源
            List<PcbaDeviceResource> pcbaDeviceResourceList = pcbaDeviceResourceDao.getAllByDeviceTypeAndSearch("", pcbaDeviceTypeId, null, 2, null, null, null, null, null, null, null, null);
            //根据 pcbaDeviceTypeId 查询 所有基线参数
            List<PcbaDeviceParam> pcbaDeviceParamList = pcbaDeviceParamDao.getAllPcbaDeviceParam(pcbaDeviceTypeId, null, null);
            //根据设备资源循环查询 模板参数填充值
            for (PcbaDeviceResource pcbaDeviceResource : pcbaDeviceResourceList) {
                Boolean statusNeedTo2 = false;
                List<PcbaDeviceRelatedDeviceParam> pcbaDeviceRelatedDeviceParamList = pcbaDeviceRelatedDeviceParamDao.getAllByDeviceResourceId(pcbaDeviceResource.getPcbaDeviceResourceId());
                // 将 pcbaDeviceRelatedDeviceParamList 和 pcbaDeviceParamList 对比 看 必填参数 是否都有值，都有值则无需修改
                for (int a = 0; a < pcbaDeviceParamList.size(); a++) {
                    for (int b = 0; b < pcbaDeviceRelatedDeviceParamList.size(); b++) {
                        PcbaDeviceRelatedDeviceParam pdrdp = pcbaDeviceRelatedDeviceParamDao.getPcbaDeviceRelatedDeviceParamByKeyAndDeviceResourceId(pcbaDeviceParamList.get(a).getPcbaDeviceParamKey(), pcbaDeviceResource.getPcbaDeviceResourceId());
                        if (pdrdp == null) {
                            statusNeedTo2 = true;
                            break;
                        } else {
                            if (pcbaDeviceParamList.get(a).getPcbaDeviceParamKey().equals(pcbaDeviceRelatedDeviceParamList.get(b).getPcbaDeviceParamKey())) {
                                if (pcbaDeviceParamList.get(a).getIsRequired() == 1) {
                                    if ("".equals(pdrdp.getPcbaDeviceParamValue())) {
                                        statusNeedTo2 = true;
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
                if (statusNeedTo2) {
                    needChangeResourceIdListTo2.add(pcbaDeviceResource.getPcbaDeviceResourceId());
                } else {
                    needChangeResourceIdListTo1.add(pcbaDeviceResource.getPcbaDeviceResourceId());
                }
            }

            // 改变这些状态
            for (Integer resourceId : needChangeResourceIdListTo2) {
                PcbaDeviceResource updateP = new PcbaDeviceResource();
                updateP.setIsFilledBaseParams(2);
                updateP.setPcbaDeviceResourceId(resourceId);
                updateP.setUpdateTime(new Date());
                pcbaDeviceResourceDao.updatePcbaDeviceResource(updateP);
            }
            for (Integer resourceId : needChangeResourceIdListTo1) {
                PcbaDeviceResource updateP = new PcbaDeviceResource();
                updateP.setIsFilledBaseParams(1);
                updateP.setPcbaDeviceResourceId(resourceId);
                updateP.setUpdateTime(new Date());
                pcbaDeviceResourceDao.updatePcbaDeviceResource(updateP);
            }
            System.out.println("触发设备资源池是否完善基线参数状态修改->2：" + needChangeResourceIdListTo2.toString());
            System.out.println("触发设备资源池是否完善基线参数状态修改->1：" + needChangeResourceIdListTo1.toString());
            return ResultUtil.success(null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }

    @Override
    public Result changeTechDeviceResourceStatus(Integer pcbaDeviceTypeId, Integer newIsFilledTechParams) {
        return null;
    }
}
